package com.cw.jeeyt.dao.impl.git;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.git.GitProjectDao;
import com.cw.jeeyt.dao.mapper.git.GitProjectMapper;
import com.cw.jeeyt.dao.model.git.GitProjectDo;
import com.cw.jeeyt.dao.model.git.GitProjectExample;
import com.cw.jeeyt.dao.model.git.GitProjectKeyDo;
import com.cw.jeeyt.dao.query.git.GitProjectQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * git项目信息数据库操作接口实现
 *
 * @author chenw
 * @date 2019-11-24 16:15:01
 */
@Repository
public class GitProjectDaoImpl extends AbstractBaseDaoImpl<GitProjectDo, GitProjectMapper> implements GitProjectDao {
    /**
     * 分页查询
     *
     * @param gitProject 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<GitProjectDo> selectPage(GitProjectQuery gitProject, PageParam pageParam) {
        GitProjectExample example = buildExample(gitProject);
        return PageHelper.<GitProjectDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询git项目信息
     *
     * @param id
     * @return
     */
    @Override
    public GitProjectDo selectByKey(Long id) {
        GitProjectKeyDo gitProjectKeyDo = new GitProjectKeyDo();
        gitProjectKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(gitProjectKeyDo);
    }

    /**
     * 根据key删除git项目信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        GitProjectKeyDo gitProjectKeyDo = new GitProjectKeyDo();
        gitProjectKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(gitProjectKeyDo);
    }

    /**
     * 查询git项目信息
     *
     * @param gitProject 条件
     * @return List<GitProjectDo>
     */
    @Override
    public List<GitProjectDo> selectByExample(GitProjectQuery gitProject) {
        return getMapper().selectByExample(buildExample(gitProject));
    }

    /**
     * 新增git项目信息
     *
     * @param gitProject 条件
     * @return int>
     */
    @Override
    public int insertBySelective(GitProjectDo gitProject) {
        if (gitProject == null) {
            return -1;
        }
        gitProject.setCreateDate(new Date());
        gitProject.setUpdateDate(new Date());
        return getMapper().insertSelective(gitProject);
    }

    /**
     * 修改git项目信息
     *
     * @param gitProject
     * @return
     */
    @Override
    public int updateBySelective(GitProjectDo gitProject) {
        if (gitProject == null) {
            return -1;
        }
        gitProject.setUpdateDate(new Date());
        return getMapper().updateByPrimaryKeySelective(gitProject);
    }

    /**
     * 构建git项目信息Example信息
     *
     * @param gitProject
     * @return
     */
    public GitProjectExample buildExample(GitProjectQuery gitProject) {
        GitProjectExample example = new GitProjectExample();
        GitProjectExample.Criteria criteria = example.createCriteria();
        if (gitProject != null) {
            //添加查询条件
            if (null != gitProject.getId()) {
                criteria.andIdEqualTo(gitProject.getId());
            }
            if (StringUtil.isNotEmpty(gitProject.getProjectCode())) {
                criteria.andProjectCodeEqualTo(gitProject.getProjectCode());
            }
            if (StringUtil.isNotEmpty(gitProject.getProjectName())) {
                criteria.andProjectNameEqualTo(gitProject.getProjectName());
            }
            if (StringUtil.isNotEmpty(gitProject.getLocalRepoPath())) {
                criteria.andLocalRepoPathEqualTo(gitProject.getLocalRepoPath());
            }
            if (StringUtil.isNotEmpty(gitProject.getRemoteRepoUri())) {
                criteria.andRemoteRepoUriEqualTo(gitProject.getRemoteRepoUri());
            }
            if (StringUtil.isNotEmpty(gitProject.getGitUsername())) {
                criteria.andGitUsernameEqualTo(gitProject.getGitUsername());
            }
            if (StringUtil.isNotEmpty(gitProject.getGitPassword())) {
                criteria.andGitPasswordEqualTo(gitProject.getGitPassword());
            }
            if (null != gitProject.getCreateBy()) {
                criteria.andCreateByEqualTo(gitProject.getCreateBy());
            }
            if (null != gitProject.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(gitProject.getCreateDateBegin());
            }
            if (null != gitProject.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(gitProject.getCreateDateEnd());
            }
            if (null != gitProject.getUpdateBy()) {
                criteria.andUpdateByEqualTo(gitProject.getUpdateBy());
            }
            if (null != gitProject.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(gitProject.getUpdateDateBegin());
            }
            if (null != gitProject.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(gitProject.getUpdateDateEnd());
            }
        }
        //添加排序字段
        example.setOrderByClause(" create_date desc");
        return example;
    }
}
